package com.ssyu.examination.assist.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyu.examination.assist.entity.AssistMsgEntity;
import com.ssyu.examination.assist.service.AssistMsgService;
import com.ssyu.examination.conmmon.App;
import com.ssyu.examination.conmmon.AppServlet;
import com.ssyu.examination.conmmon.Constants;
import com.ssyu.examination.conmmon.PageModel;
import com.ssyu.examination.conmmon.util.DBUtil;
import com.ssyu.examination.conmmon.util.FastjsonUtil;
import com.ssyu.examination.system.entity.ClazzEntity;
import com.ssyu.examination.system.entity.SystemEntiy;

@WebServlet("/AssistMsgServlet")
public class AssistMsgServlet extends AppServlet {
	private static final long serialVersionUID = 1L;

	public AssistMsgServlet() {
		super();
	}

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("listMsg".equals(command)) {
			listMsg(request, response);
		} else if ("deleteMsg".equals(command)) {
			deleteMsg(request, response);
		} else if ("effectMsg".equals(command)) {
			effectMsg(request, response);
		} else if ("detailMsg".equals(command)) {
			detailMsg(request, response);
		} else if ("addMsg".equals(command)) {
			addMsg(request, response);
		} else if ("updateMsg".equals(command)) {
			updateMsg(request, response);
		} else if ("listPerson".equals(command)) {
			listPerson(request, response);
		} else if ("detailPersonMsg".equals(command)) {
			detailPersonMsg(request, response);
		} else if ("listClazz".equals(command)) {
			listClazz(request, response);
		}

	}

	/**
	 * 分页站内消息查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		String title = request.getParameter("msgSerachTitle");

		String style = request.getParameter("msgSerachStyle");

		String role = request.getParameter("msgSerachRole");
		String stateStr = request.getParameter("msgSerachState");

		int state = 0;
		if (stateStr != null) {
			state = Integer.parseInt(stateStr);
		}

		String time = request.getParameter("msgSerachTime");

		AssistMsgEntity assistMsgEntity = new AssistMsgEntity();
		assistMsgEntity.setTitle(title);
		assistMsgEntity.setStyle(style);
		assistMsgEntity.setSysRole__name(role);
		assistMsgEntity.setState(state);
		assistMsgEntity.setExpireTime(time);

		String code = App.getSystemCode(request, response);

		AssistMsgService assistMsgService = new AssistMsgService();
		PageModel<AssistMsgEntity> pageModel = assistMsgService.listMsg(page, rows, assistMsgEntity, code, state);
		print(response, FastjsonUtil.toJsonWithDateFormat(pageModel, "yyyy-MM-dd HH:mm:ss"));
	}

	/**
	 * 删除站内信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void deleteMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		AssistMsgService assistMsgService = new AssistMsgService();

		int result = assistMsgService.deleteMsg(id);
		if (result != 0) {
			response.getWriter().print(1);
		}
	}

	/**
	 * 站内信息生效操作
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void effectMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		AssistMsgService assistMsgService = new AssistMsgService();

		int result = assistMsgService.effectMsg(id);
		if (result != 0) {
			response.getWriter().print(1);
		}
	}

	/**
	 * 站内消息详情查看
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void detailMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		AssistMsgService assistMsgService = new AssistMsgService();

		List<SystemEntiy> detailPersonMsg = assistMsgService.detailPersonMsg(id);
		AssistMsgEntity assistMsgEntity = assistMsgService.detailMsg(id);
		List<Object> manageList = new ArrayList<>();
		manageList.add(detailPersonMsg);
		manageList.add(assistMsgEntity);

		// System.out.println(FastjsonUtil.toJson(detailPersonMsg));

		request.getSession().setAttribute("assistMsgEntity", assistMsgEntity);
		if (assistMsgEntity != null) {
			print(response, FastjsonUtil.toJson(manageList));
		}
	}

	/**
	 * 新增站内信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void addMsg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String expireTime = request.getParameter("expireTime");

		String[] receiveMan = request.getParameterValues("receiveMan");
		List<String> tmp = new ArrayList<String>();
		if (receiveMan == null) {
			response.getWriter().print(2);
			return;
		}
		for (String str : receiveMan) {
			if (str != null && str.length() != 0) {
				tmp.add(str);
			}
		}
		receiveMan = tmp.toArray(new String[0]);
		// String receiveClazz = request.getParameter("receiveClazz");
		int state = Integer.parseInt(request.getParameter("state"));

		// System.out.println(receiveMan);
		// for (int i = 0; i < receiveMan.length; i++) {
		// System.out.println(i + receiveMan[i] + "len" + receiveMan.length);
		// }

		AssistMsgEntity assistMsgEntity = new AssistMsgEntity();
		assistMsgEntity.setTitle(title);
		assistMsgEntity.setContent(content);
		assistMsgEntity.setExpireTime(expireTime);
		// assistMsgEntity.setClass__name(receiveClazz);
		assistMsgEntity.setState(state);

		AssistMsgService assistMsgService = new AssistMsgService();

		int result = assistMsgService.addMsg(assistMsgEntity, state, receiveMan, App.getSystemCode(request, response));
		if (result != 0) {
			response.getWriter().print(1);
		}
	}

	/**
	 * 修改站内消息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void updateMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int id = Integer.parseInt(request.getParameter("id"));
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String expireTime = request.getParameter("expireTime");
		// String receiveMan = request.getParameter("receiveMan");
		String[] receiveMan = request.getParameterValues("msgUpdateFormReceiveMan");
		Set<String> tmp = new HashSet<String>();

		for (String str : receiveMan) {
			if (str != null && str.length() != 0) {
				tmp.add(str);
			}
		}
		receiveMan = tmp.toArray(new String[0]);

		int state = Integer.parseInt(request.getParameter("state"));

		// for (int i = 0; i < receiveMan.length; i++) {
		// System.out.println(i + receiveMan[i] + "len" + receiveMan.length);
		// }

		AssistMsgEntity assistMsgEntity = new AssistMsgEntity();
		assistMsgEntity.setTitle(title);
		assistMsgEntity.setContent(content);
		assistMsgEntity.setExpireTime(expireTime);
		// assistMsgEntity.setClass__name(receiveMan);
		// assistMsgEntity.setClass__name(receiveClazz);
		assistMsgEntity.setState(state);

		AssistMsgService assistMsgService = new AssistMsgService();

		response.setContentType("text/html;charset=utf-8");
		int result = assistMsgService.updateMsg(assistMsgEntity, state, receiveMan,
				App.getSystemCode(request, response), id);
		if (result != 0) {
			response.getWriter().print(1);
		}
	}

	/**
	 * 列举出人
	 * 
	 * @param request
	 * @param response
	 */
	private void listPerson(HttpServletRequest request, HttpServletResponse response) {
		List<SystemEntiy> assistMsgEntity = DBUtil.executeQuery(SystemEntiy.class,
				"SELECT u.*,b.name AS 'class__name' FROM sys_user u LEFT JOIN (SELECT name,id FROM sys_class) b ON b.id = u.class_id");
		int len = assistMsgEntity.size();
		for (int i = 0; i < len; i++) {
			String namei = assistMsgEntity.get(i).getName();
			String codei = assistMsgEntity.get(i).getCode();
			String clazzi = assistMsgEntity.get(i).getClass__name();
			if (clazzi != null) {
				namei = namei + "(" + clazzi + ",code:" + codei + ")";
			}
			assistMsgEntity.get(i).setName(namei);
		}
		// for (int i = 0; i < len; i++) {
		// for (int j = i + 1; j < len; j++) {
		// String namei = assistMsgEntity.get(i).getName();
		// String namej = assistMsgEntity.get(j).getName();
		//
		// String codei = assistMsgEntity.get(i).getCode();
		// String codej = assistMsgEntity.get(j).getCode();
		//
		// String clazzi = assistMsgEntity.get(i).getClass__name();
		// String clazzj = assistMsgEntity.get(j).getClass__name();
		// if (namei.equals(namej)) {
		// namei = namei + "(" + clazzi + ",code:" + codei + ")";
		// System.out.println("i" + namei);
		// namej = namej + "(" + clazzj + ",code:" + codej + ")";
		// System.out.println("j" + namej);
		// assistMsgEntity.get(i).setName(namei);
		// assistMsgEntity.get(j).setName(namej);
		// }else {
		// namei
		// }
		// }
		// }
		// System.out.println(FastjsonUtil.toJson(assistMsgEntity));
		if (assistMsgEntity != null) {
			print(response, FastjsonUtil.toJson(assistMsgEntity));
		}
	}

	private void detailPersonMsg(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		AssistMsgService assistMsgService = new AssistMsgService();

		List<SystemEntiy> detailPersonMsg = assistMsgService.detailPersonMsg(id);
		for (int i = 0; i < detailPersonMsg.size(); i++) {
			String namei = detailPersonMsg.get(i).getName();
			String codei = detailPersonMsg.get(i).getCode();
			String clazzi = detailPersonMsg.get(i).getClass__name();
			if (clazzi != null) {
				namei = namei + "(" + clazzi + ",code:" + codei + ")";
			}
			detailPersonMsg.get(i).setName(namei);
		}
		// System.out.println(FastjsonUtil.toJson(detailPersonMsg));
		if (detailPersonMsg != null) {
			print(response, FastjsonUtil.toJson(detailPersonMsg));
		}
	}

	/**
	 * 列举出班级
	 * 
	 * @param request
	 * @param response
	 */
	private void listClazz(HttpServletRequest request, HttpServletResponse response) {
		List<ClazzEntity> assistMsgEntity = DBUtil.executeQuery(ClazzEntity.class, "SELECT * FROM sys_class");
		if (assistMsgEntity != null) {
			print(response, FastjsonUtil.toJson(assistMsgEntity));
		}
	}

}
